package com.tongji.seller.service
{
	
	import com.tongji.seller.events.AddImageEvent;
	import com.tongji.seller.events.AddStoreSceneEvent;
	import com.tongji.seller.events.GetStoreSceneDetailEvent;
	import com.tongji.seller.events.GetStoreUserSceneEvent;
	import com.tongji.seller.model.UserModel;
	
	import flash.system.Security;
	
	import mx.collections.ArrayCollection;
	import mx.controls.Alert;
	import mx.rpc.events.FaultEvent;
	import mx.rpc.events.ResultEvent;
	import mx.rpc.soap.WebService;
	
	import org.robotlegs.mvcs.Actor;
	
	import spark.components.mediaClasses.VolumeBar;
	
	/**
	 * 
	 * @author yanghanbing
	 * 2011/08/
	 */ 
	public class UserSceneService extends Actor
	{
		private var ws:WebService;

		[Inject]
		public var userModel:UserModel;
		
		public function UserSceneService()
		{
			super();
			ws = new WebService();
			ws.wsdl = "http://share.rryjw.com/VRService.asmx?wsdl";
			ws.useProxy = false;
			ws.loadWSDL();
			Security.loadPolicyFile("http://img.rryjw.com/crossdomain.xml");
		}
		/**
		 * 用户保存场景时，保存的场景图片 
		 * @param images
		 * 
		 */		
		public function addImages(images:ArrayCollection):void
		{
			ws.AddImgList(images);
			ws.AddImgList.addEventListener(ResultEvent.RESULT, addImagesResult);
			ws.AddImgList.addEventListener(FaultEvent.FAULT, faultHandler);
		}
		
		public function addPngImg(imgs:ArrayCollection):void
		{
			ws.AddImgListPng(imgs);
			ws.AddImgListPng.addEventListener(ResultEvent.RESULT, addPngImagesResult);
			ws.AddImgListPng.addEventListener(FaultEvent.FAULT, faultHandler);
		}
		/**
		 * 添加店铺搭配的用户场景
		 * @param userId
		 * @param scjId
		 * 
		 */		
		public function addStoreUserScene(spara:String):void
		{
			ws.AddStoreUserScene(spara);
			ws.AddStoreUserScene.addEventListener(ResultEvent.RESULT, addUserSceneResult);
			ws.AddStoreUserScene.addEventListener(FaultEvent.FAULT, faultHandler);
		}
		
		/**
		 * 获取店铺搭配的用户场景
		 * @param userId
		 * @param page_no
		 * @param pageSize
		 * 
		 */				
		public function getStoreUserScene(userId:Number, page_no:int, pageSize:int):void
		{
			ws.GetStoreUserScene(userId, page_no, pageSize);
			ws.GetStoreUserScene.addEventListener(ResultEvent.RESULT, getStoreUserSceneResult);
			ws.GetStoreUserScene.addEventListener(FaultEvent.FAULT, faultHandler);
		}
		
		private function addImagesResult(event:ResultEvent):void
		{
			dispatch(new AddImageEvent(AddImageEvent.UPDATE_IMAGE, event.result as ArrayCollection));
		}
		
		private function addPngImagesResult(event:ResultEvent):void
		{
			dispatch(new AddImageEvent(AddImageEvent.UPDATE_PNG_IMG, event.result as ArrayCollection));
		}
		private function addUserSceneResult(event:ResultEvent):void
		{
			if (event.result)
			{
				dispatch(new AddStoreSceneEvent(AddStoreSceneEvent.ADD_USER_SCENE_SUCCESS, 1));
				dispatch(new GetStoreUserSceneEvent(GetStoreUserSceneEvent.GET_STORE_USER_SCENE, userModel.currentUserID, 1, 15));
			}				
			else
				Alert.show("您保存场景出错啦！");
		}
		
		private function getStoreUserSceneResult(event:ResultEvent):void
		{
			dispatch( new GetStoreUserSceneEvent(GetStoreUserSceneEvent.UPDATE_STORE_USER_SCENE, 0, 0, 0, event.result as String));
		}
		
		private function faultHandler(event:FaultEvent):void
		{
			Alert.show("失败" + event.fault.toString());
		}
	}
}